<?php

/**
 * Description of Route
 *
 * @author a.novikov, dizews
 */
class Route extends Zend_Controller_Router_Route_Module {

	public function assemble($data = array(), $reset = false, $encode = true, $partial = false) {
		$url = parent::assemble($data, $reset, $encode, $partial);
		$lastChar = strlen($url) - 1;
		if ($url[$lastChar] != '/') {
			$url .= '/';
		}
		return $url;
	}
	
	
	public function match($path, $partial = false) {
		$this->_setRequestKeys();

		$values = array();
		$params = array();

		if (!$partial) {
			$path = trim($path, self::URI_DELIMITER);
		} else {
			$matchedPath = $path;
		}

		if ($path != '') {
			$path = explode(self::URI_DELIMITER, $path);

			if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
				$values[$this->_moduleKey] = array_shift($path);
				$this->_moduleValid = true;
			}

			if (count($path) && !empty($path[0])) {
				$values[$this->_controllerKey] = array_shift($path);
			}

			if (count($path) && !empty($path[0])) {
				$values[$this->_actionKey] = array_shift($path);
			}

			$numSegs = count($path);
			if ($numSegs) {
				for ($i = 0; $i < $numSegs; $i = $i + 2) {
					$key = urldecode($path[$i]);
					$val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
					/**
					 * @todo ради этой строки и был переопределено метод зенда
					 *	!!!ВНИМАНИЕ!!! возможно небудут работать урлы вида: val1/val2/val3/...
					 */
					if (empty($val)) {
						return false;
					}
					$params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
				}
			}
		}

		if ($partial) {
			$this->setMatchedPath($matchedPath);
		}

		$this->_values = $values + $params;

		return $this->_values + $this->_defaults;
	}
}